package helloworld;

import com.rabbitmq.client.*;
import org.junit.Test;
import util.RabbitUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Customer {


  public static void main(String[] args) throws IOException, TimeoutException {
    //创建连接mg的连接工厂对象
//    ConnectionFactory connectionFactory = new ConnectionFactory();
//    connectionFactory.setHost ( "106.55.243.100" );//设置端口号
//    connectionFactory.setPort (5672);//设置连接那个虚拟主机
//    connectionFactory.setVirtualHost ( "/ems" );//设置访问虚拟主机的用户名和密码
//    connectionFactory.setUsername ( "ems" );
//    connectionFactory.setPassword ( "123");//获取连接对象


    //创建连接对象
//    Connection connection = connectionFactory.newConnection();

    Connection connection = RabbitUtil.getConnection();

    //获取连接通道
    Channel channel = connection.createChannel();
    //通道绑定队列
    channel.queueDeclare( "hello" , true, false,  true ,  null);
    //消费消息
    //参数1:消费那个队列的消息队列名称
    //参数2:开始消息的自动确认机制
    //参数3:消费时的回调接口
    channel.basicConsume("hello",true,new DefaultConsumer(channel){
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        System.out.println("====");
        System.out.println("new String(body):" + new String(body));
      }
    });

//    channel.close();
//    connection.close();
  }
}
